// HtmlApp.cpp : Defines the entry point for the console application.
//
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>

#include <acme/windows.h>

#include <acme/scratch/ScratchWindowsFramework.h>
#include <acme/scratch/ScratchActiveX.h>
#include <acme/scratch/ScratchConsole.h>

using namespace acme::scratch;
using namespace acme::windows;
#include <ExDispid.h>

static PARAMDATA ExternalApiParamData[]={
  {L"",VT_BSTR},
};
static METHODDATA ExternalApiMethodData[] = {
  { L"HelloWorld",     &ExternalApiParamData[0],  100,     0,  CC_CDECL,  1,  DISPATCH_METHOD,  VT_EMPTY}
};
static INTERFACEDATA ExternalApiInterfaceData =
{
  ExternalApiMethodData,_countof(ExternalApiMethodData)
};

struct MyExternalApi
{
  virtual void __cdecl HelloWorld(BSTR string){}
};

//class MyWindow;
// MyFrame is a detatched object

class MyWindow : public WindowDelegate,
  public WebBrowserEventsDelegate,
  public MyExternalApi,
  public ActiveXFrame
{
  Window window;
  DynamicRef<WebView> webView;
  friend class MyFrameProxy;
public:
  MyWindow(){}
  void Create()
  {
    Rect rect(CW_USEDEFAULT,0,CW_USEDEFAULT,0);
    window.initWithContentRect(rect);
    window.makeKeyAndOrderFront();
    window.setDelegate(this);

    Rect frame(100,100,256,256);
    webView->initWithFrame(frame,this,this,(MyExternalApi*)this,&ExternalApiInterfaceData);
//    window.setContentView(&webView);
    webView->loadRequest(TEXT("file://c:\\Projects\\Acme\\HtmlApp\\res\\default.htm"));
  }

  virtual void __cdecl HelloWorld(BSTR string)
  {
    Console con;
    con.Write(TEXT("MyWindow.HelloWorld!"));
  }

  // WebBrowserEventsDelegate methods
  virtual void __cdecl BeforeNavigate2(IDispatch* pDisp, VARIANT* url,VARIANT* flags,VARIANT* targetFrame,VARIANT* postData,VARIANT* headers,VARIANT_BOOL* Cancel) override
  {
    Trace::Write(TEXT("MyWindow <- BeforeNavigate2"));
  }
  virtual void __cdecl CommandStateChange(INT commandState,BOOL fEnabled)
  {
    Trace::Write(TEXT("MyWindow <- CommandStateChange"));
  }
  virtual void __cdecl ProgressChange(INT,INT)
  {
    Trace::Write(TEXT("MyWindow <- ProgressChange"));
  }
  virtual void __cdecl PropertyChange(BSTR bstr) override
  {
    Trace::Write(TEXT("MyWindow <- PropertyChange"));
  }
  virtual void __cdecl DownloadBegin() override
  {
    Trace::Write(TEXT("MyWindow <- DownloadBegin"));
  }
  virtual void __cdecl StatusTextChange(BSTR prop) override
  {
    Trace::Write(TEXT("MyWindow <- StatusTextChange"));
  }
  virtual void __cdecl FileDownload(VARIANT_BOOL,VARIANT_BOOL*) override
  {
    Trace::Write(TEXT("MyWindow <- FileDownload"));
  }
  virtual HRESULT STDMETHODCALLTYPE GetWindow(HWND *phwnd)override
  {
    *phwnd = window.hwnd();
    return *phwnd?S_OK:E_FAIL;
  }
  virtual void windowDidResize(int width,int height) override
  {
    webView->resizeTo(0,0,width,height);
  }
  static void CALLBACK PostKillMessageProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime)
  {
    PostQuitMessage(0);
  }
  virtual void windowDidClose() override
  {
    webView->close();
    HWND hwnd = window.hwnd();
    SetTimer(0,0,1000,&PostKillMessageProc);
  }
  virtual void windowTimer(int id) override
  {
    PostQuitMessage(0);
  }
};

class MyApp : public ApplicationDelegate
{
  DynamicRef<MyWindow> window;
public:
  MyApp(){}
  void applicationDidFinishLaunching() override
  {
//    INTERNETFEATURELIST 
//    CoInternetSetFeatureEnabled(,SET_FEATURE_ON_PROCESS,

    window->Create();
  }
};

int main()
{
	int result = STApplication::Main(&MyApp());
  _CrtDumpMemoryLeaks();
  return result;
}
